package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.DistributedLock;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.ActivityTaskPanelDao;
import com.sc.nft.dao.ActivityTaskPrizeDao;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dto.ActivityTaskPlanAdminDTO;
import com.sc.nft.dto.ActivityTaskPrizeDTO;
import com.sc.nft.entity.ActivityTask;
import com.sc.nft.entity.ActivityTaskPanel;
import com.sc.nft.entity.ActivityTaskPrize;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.dto.AdminModifierGroupDTO;
import com.sc.nft.entity.dto.ImCreateGroupDTO;
import com.sc.nft.entity.vo.ActivityTaskDetailVO;
import com.sc.nft.entity.vo.ActivityTaskPanelVO;
import com.sc.nft.entity.vo.ActivityTaskSimpleVO;
import com.sc.nft.entity.vo.AdminGroupListVO;
import com.sc.nft.enums.task.TaskPrizeTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ActivityTaskPanelService;
import com.sc.nft.service.ActivityTaskPlanService;
import com.sc.nft.service.ActivityTaskService;
import com.sc.nft.service.ImGroupService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/activityTask")
@Api(tags = "活动任务管理")
public class ActivityTaskAdminController {

    private final ActivityTaskService activityTaskService;
    private final ActivityTaskPlanService activityTaskPlanService;
    private final ActivityTaskPanelService activityTaskPanelService;
    private final ActivityTaskPanelDao activityTaskPanelDao;
    private final ActivityTaskPrizeDao activityTaskPrizeDao;
    private final EquityPropsDao equityPropsDao;


    @PostMapping(value = "/panelList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "活动任务模板", notes = "活动任务模板", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<ActivityTaskPanelVO>> panelList() {
        return Result.successColl(activityTaskPanelService.getList(),ActivityTaskPanelVO.class);
    }


    @PostMapping(value = "/effPanelList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "生效中活动任务模板", notes = "生效中活动任务模板", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<ActivityTaskPanelVO>> effPanelList() {
        return Result.successColl(activityTaskPanelService.getEffectList(),ActivityTaskPanelVO.class);
    }

    @PostMapping(value = "/updateStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "模板启用/禁用", notes = "模板启用/禁用", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result  updateStatus(@Valid @RequestBody ActivityTaskStatusRequest request) {
        ActivityTaskPanel taskPanel = activityTaskPanelDao.getById(request.getId());
        Assert.notNull(taskPanel,()->new GlobalRunTimeException("未找到任务模板"));
        taskPanel.setStatus(request.getStatus());
        taskPanel.updateById();
        return Result.success();
    }


    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加活动任务", notes = "添加活动任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result add(@Valid @RequestBody AddActivityTaskRequest request) {
        activityTaskService.addTask(request.getName(),request.getTaskPanelId(),request.getBuyCollectionId(),
                request.getBuyCount(),request.getPartakeCollectionId(),request.getParticipants(),request.getCollectionRange(),
                request.getMallRange(),request.getTaskPlan(),request.getRepeatFinish(),request.getTaskPrizeDTOS(),request.getSort(),request.getSpendPrice());
        return Result.success();
    }
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑活动任务", notes = "编辑活动任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result edit(@Valid @RequestBody EditActivityTaskRequest request) {
        activityTaskService.editTask(request.getId(),request.getName(),request.getTaskPanelId(),request.getBuyCollectionId(),
                request.getBuyCount(),request.getPartakeCollectionId(),request.getParticipants(),request.getCollectionRange(),
                request.getMallRange(),request.getTaskPlan(),request.getRepeatFinish(),request.getTaskPrizeDTOS(),request.getSort(),request.getSpendPrice());
        return Result.success();
    }

    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "任务列表", notes = "任务列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<ActivityTaskSimpleVO> page(@Valid @RequestBody PageRequest request) {
        return PageResult.success(activityTaskService.getPage(request.toMp()),ActivityTaskSimpleVO.class);
    }


    @PostMapping(value = "/recordPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "任务记录", notes = "任务记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<ActivityTaskPlanAdminDTO> recordPage(@Valid @RequestBody ActivityTaskRecordRequest request) {
        return PageResult.success(activityTaskPlanService.adminTaskRecord(request.toMp(),request.getUserTel(),request.getActivityId(),request.getTaskName()));
    }

    @PostMapping(value = "/updateSort", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排序", notes = "修改排序", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateSort(@Valid @RequestBody SortRequest request) {
        ActivityTask task = activityTaskService.getById(request.getId());
        if(ObjectUtil.isNull(task)){
            return Result.fail("未找到任务");
        }
        task.setSort(request.getSort());
        task.updateById();
        return Result.success();
    }


    @PostMapping(value = "/remove", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除活动任务", notes = "添加活动任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result remove(@Valid @RequestBody IdRequest request) {
        ActivityTask task = activityTaskService.getById(request.getId());
        if(ObjectUtil.isNull(task)){
            return Result.fail("未找到任务");
        }
        task.deleteById();
        return Result.success();
    }
    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "任务详情", notes = "任务详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ActivityTaskDetailVO> page(@Valid @RequestBody IdRequest request) {
        ActivityTask task = activityTaskService.getById(request.getId());
        if(ObjectUtil.isNull(task)){
            return Result.fail("未找到任务");
        }
        ActivityTaskDetailVO activityTaskDetailVO = BeanUtil.toBean(task,ActivityTaskDetailVO.class);
        List<ActivityTaskPrize> activityTaskPrizes = activityTaskPrizeDao.getByTaskId(task.getId());
        List<ActivityTaskPrizeDTO> activityTaskPrizeDTOS = Lists.newArrayList();
        for (ActivityTaskPrize activityTaskPrize : activityTaskPrizes) {
            ActivityTaskPrizeDTO activityTaskPrizeDTO = new ActivityTaskPrizeDTO();
            activityTaskPrizeDTO.setType(activityTaskPrize.getType());
            if(activityTaskPrize.getType()== TaskPrizeTypeEnum.PROPS) {
                activityTaskPrizeDTO.setPropsId(activityTaskPrize.getEquityId());
                activityTaskPrizeDTO.setPropsName(activityTaskPrize.getEquityName());
            }
            activityTaskPrizeDTO.setPrize(activityTaskPrize.getPrize());
            activityTaskPrizeDTOS.add(activityTaskPrizeDTO);
        }
        activityTaskDetailVO.setTaskPrizeDTOS(activityTaskPrizeDTOS);
        return Result.success(activityTaskDetailVO);
    }


    @PostMapping(value = "/simpleList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "简单活动任务列表", notes = "简单活动任务列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result simpleList() {
        List<ActivityTask> task = activityTaskService.list();
        return Result.successColl(task,ActivityTaskSimpleVO.class);
    }
}
